package com.example.blog.controller;

import com.example.blog.dto.LikeDTO;
import com.example.blog.service.LikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/likes")
@CrossOrigin(origins = "*", maxAge = 3600)
public class LikeController {
    @Autowired
    private LikeService likeService;

    @PostMapping("/articles/{articleId}")
    @PreAuthorize("hasRole('USER')")
    public ResponseEntity<LikeDTO> likeArticle(@PathVariable Long articleId) {
        return ResponseEntity.ok(likeService.likeArticle(articleId));
    }

    @DeleteMapping("/articles/{articleId}")
    @PreAuthorize("hasRole('USER')")
    public ResponseEntity<?> unlikeArticle(@PathVariable Long articleId) {
        likeService.unlikeArticle(articleId);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/articles/{articleId}/status")
    @PreAuthorize("hasRole('USER')")
    public ResponseEntity<Boolean> hasLiked(@PathVariable Long articleId) {
        return ResponseEntity.ok(likeService.hasLiked(articleId));
    }

    @GetMapping("/users/{userId}")
    public ResponseEntity<Page<LikeDTO>> getUserLikes(
            @PathVariable Long userId,
            Pageable pageable) {
        return ResponseEntity.ok(likeService.getUserLikes(userId, pageable));
    }

    @GetMapping("/articles/{articleId}")
    public ResponseEntity<Page<LikeDTO>> getArticleLikes(
            @PathVariable Long articleId,
            Pageable pageable) {
        return ResponseEntity.ok(likeService.getArticleLikes(articleId, pageable));
    }

    @GetMapping("/articles/{articleId}/count")
    public ResponseEntity<Long> getArticleLikesCount(@PathVariable Long articleId) {
        return ResponseEntity.ok(likeService.getArticleLikesCount(articleId));
    }
} 